package com.coocaa.typhoon.api.v1.elasticsearch;

import com.coocaa.cirrus.common.base.model.RespBase;
import com.coocaa.typhoon.api.third.elasticsearch.model.UserFileDoc;
import com.coocaa.typhoon.api.third.elasticsearch.service.UserFileEsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2025/10/21 15:35
 * @description
 */
@RestController
@RequestMapping("/v1/es")
@Tag(name = "Elasticsearch测试")
public class TestElasticsearchController {
    @Autowired
    private UserFileEsService userFileEsService;


    @Operation(summary = "测试保存单个")
    @PostMapping(value = "/testSave")
    public RespBase testSave(@RequestBody UserFileDoc entity) {
        userFileEsService.save(entity);
        return new RespBase().setSuccessResult();
    }

    @Operation(summary = "测试保存批量")
    @PostMapping(value = "/testSaveBatch")
    public RespBase testSaveBatch(@RequestBody List<UserFileDoc> UserFileDocList) {
        userFileEsService.saveAll(UserFileDocList);
        return new RespBase().setSuccessResult();
    }

    @Operation(summary = "测试查询")
    @PostMapping(value = "/testQuery")
    @Parameters({
            @Parameter(name = "userId", example = "1", description = "用户ID")
    })
    public RespBase<List<UserFileDoc>> testQuery(@RequestParam String userId) {
        List<UserFileDoc> res = userFileEsService.findByUserId(userId);
        return new RespBase<List<UserFileDoc>>().setData(res).setSuccessResult();
    }

    @Operation(summary = "测试删除")
    @PostMapping(value = "/testDel")
    @Parameters({
            @Parameter(name = "userId", example = "1", description = "用户ID")
    })
    public RespBase<List<UserFileDoc>> testDel(@RequestParam String userId) {
        userFileEsService.deleteById(userId);
        return new RespBase().setSuccessResult();
    }

    @Operation(summary = "测试分页")
    @PostMapping(value = "/testPage")
    @Parameters({
            @Parameter(name = "fileType", example = "1", description = "文件类型")
    })
    public RespBase<Object> testPage(@RequestParam String fileType) {
        Page<UserFileDoc> res = userFileEsService.pageByFileType(fileType, 0, 10);
        return new RespBase().setData(res).setSuccessResult();
    }
}
